package org.suyafei.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.suyafei.manager.ShortLinkManager;
import org.suyafei.mapper.ShortLinkMapper;
import org.suyafei.model.ShortLinkDO;

/**
 * @author 宿某人
 * @Date: 2021/12/21/3:55 下午
 * @Description:
 */
@Component
public class ShortLinkManagerImpl implements ShortLinkManager {

    @Autowired
    private ShortLinkMapper shortLinkMapper;
    @Override
    public int addShortLink(ShortLinkDO shortLinkDO) {
        return shortLinkMapper.insert(shortLinkDO);
    }

    @Override
    public int del(String shortLinkCode, Long accountNo) {
        ShortLinkDO shortLinkDO = new ShortLinkDO();
        shortLinkDO.setDel(1);
        return shortLinkMapper.update(shortLinkDO,new QueryWrapper<ShortLinkDO>().eq("code", shortLinkCode));
    }

    @Override
    public int update(ShortLinkDO shortLinkDO) {
        return shortLinkMapper.update(null,new UpdateWrapper<ShortLinkDO>().eq("code", shortLinkDO.getCode()).eq("del", 0)
                .set("title",shortLinkDO.getTitle())
                .set("domain",shortLinkDO.getDomain()));
    }

    @Override
    public ShortLinkDO findByShortLinkCode(String code) {
        return shortLinkMapper.selectOne(new QueryWrapper<ShortLinkDO>()
                .eq("code", code).eq("del", 0));
    }
}
